<template>
  <div class="admin-layout">
    <aside class="admin-sidebar">
      <div class="sidebar-header">
        <h3>物联网管理平台</h3>
        <p>智慧农场后台</p>
      </div>

      <nav class="admin-nav">
        <router-link
            v-for="route in adminRoutes"
            :key="route.path"
            :to="route.path"
            class="nav-item"
            :class="{ active: $route.path.startsWith(route.path) }"
        >
          <span class="nav-icon">{{ route.meta.icon }}</span>
          <div class="nav-content">
            <span class="nav-text">{{ route.meta.title }}</span>
            <span class="nav-description">{{ route.meta.description }}</span>
          </div>
        </router-link>
      </nav>
    </aside>

    <div class="admin-main">
      <header class="admin-header">
        <div class="header-left">
          <h3>{{ currentRouteTitle }}</h3>
          <p class="header-description">{{ currentRouteDescription }}</p>
        </div>

        <div class="header-right">
          <button @click="goToFarm" class="btn-farm">
            🏠 返回农场系统
          </button>
          <div class="user-info">
            <div class="user-avatar" @click="goToUserProfile">
              {{ authStore.user?.username?.charAt(0).toUpperCase() || 'A' }}
            </div>
            <div class="user-details">
              <div class="user-name">{{ authStore.user?.username || '管理员' }}</div>
              <div class="user-role">{{ authStore.user?.role || '管理员' }}</div>
            </div>
            <button @click="handleLogout" class="btn-logout">
              退出
            </button>
          </div>
        </div>
      </header>

      <main class="admin-content">
        <router-view></router-view>
      </main>
    </div>
  </div>
</template>

<script>
import { useAuthStore } from '@/stores/auth'
import { useRouter, useRoute } from 'vue-router'
import { computed } from 'vue'

export default {
  name: 'AdminLayout',
  setup() {
    const authStore = useAuthStore()
    const router = useRouter()
    const route = useRoute()

    // 定义与 router/index.js 一致的路由配置 - 包含用户权限管理和设备管理
    const adminRoutes = [
      {
        path: '/admin/users',  // 用户权限管理路径
        meta: {
          title: '用户权限管理',
          description: '管理不同角色：管理员、农户、养殖户',
          icon: '👥'
        }
      },
      {
        path: '/admin/devices', // 设备管理路径（与用户管理同层级）
        meta: {
          title: '设备管理',
          description: '管理物联网设备：状态、配置、数据监控',
          icon: '🔧' // 设备相关图标，可根据需求替换
        }
      }
    ]

    const currentRouteTitle = computed(() => {
      return route.meta.title || '后台管理系统'
    })

    const currentRouteDescription = computed(() => {
      return route.meta.description || '物联网平台管理'
    })

    const goToFarm = () => {
      router.push('/dashboard/smart-home')
    }

    const goToUserProfile = () => {
      router.push('/user-profile')
    }

    const handleLogout = () => {
      authStore.logout()
      router.push('/login')
    }

    return {
      authStore,
      adminRoutes,
      currentRouteTitle,
      currentRouteDescription,
      goToFarm,
      goToUserProfile,
      handleLogout
    }
  }
}
</script>

<style scoped>
.admin-layout {
  display: flex;
  min-height: 100vh;
  background: #f8f9fa;
}

.admin-sidebar {
  width: 280px;
  background: linear-gradient(180deg, #1e3c72, #2a5298);
  color: white;
  padding: 0;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
}

.sidebar-header {
  padding: 25px 20px;
  text-align: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  margin-bottom: 10px;
}

.sidebar-header h3 {
  margin: 0 0 8px 0;
  font-size: 1.4rem;
  font-weight: 600;
}

.sidebar-header p {
  margin: 0;
  opacity: 0.8;
  font-size: 0.9rem;
}

.admin-nav {
  padding: 15px 0;
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 15px 20px;
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  transition: all 0.3s ease;
  border-left: 4px solid transparent;
  cursor: pointer;
}

.nav-item:hover {
  background: rgba(255, 255, 255, 0.1);
  color: white;
  border-left-color: rgba(255, 255, 255, 0.3);
}

.nav-item.active {
  background: rgba(255, 255, 255, 0.15);
  border-left-color: #48bb78;
  color: white;
}

.nav-icon {
  font-size: 1.3rem;
  margin-right: 15px;
  width: 24px;
  text-align: center;
}

.nav-content {
  display: flex;
  flex-direction: column;
  flex: 1;
}

.nav-text {
  font-weight: 500;
  font-size: 0.95rem;
  margin-bottom: 2px;
}

.nav-description {
  font-size: 0.75rem;
  opacity: 0.7;
  line-height: 1.3;
}

.admin-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0; /* 防止flex内容溢出 */
}

.admin-header {
  background: white;
  padding: 18px 30px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #e8ecef;
  flex-shrink: 0; /* 防止header被压缩 */
}

.header-left h3 {
  margin: 0 0 5px 0;
  color: #2c3e50;
  font-size: 1.5rem;
  font-weight: 600;
}

.header-description {
  margin: 0;
  font-size: 0.9rem;
  color: #6c757d;
  opacity: 0.8;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.btn-farm {
  background: linear-gradient(135deg, #48bb78, #2d5a27);
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  transition: all 0.3s ease;
  box-shadow: 0 2px 6px rgba(72, 187, 120, 0.3);
}

.btn-farm:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(72, 187, 120, 0.4);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  border-radius: 8px;
  transition: background-color 0.3s ease;
}

.user-info:hover {
  background: #f8f9fa;
}

.user-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 1.1rem;
  cursor: pointer;
  transition: all 0.3s ease;
}

.user-avatar:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.user-details {
  display: flex;
  flex-direction: column;
  cursor: pointer;
}

.user-name {
  font-weight: 600;
  color: #2c3e50;
  font-size: 0.95rem;
}

.user-role {
  font-size: 0.8rem;
  color: #6c757d;
  margin-top: 2px;
}

.btn-logout {
  background: #e74c3c;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
  transition: all 0.3s ease;
  margin-left: 8px;
}

.btn-logout:hover {
  background: #c0392b;
  transform: translateY(-1px);
}

.admin-content {
  flex: 1;
  padding: 25px;
  overflow-y: auto;
  background: #f8f9fa;
  min-height: 0; /* 重要：允许内容区域滚动 */
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .admin-sidebar {
    width: 240px;
  }

  .sidebar-header {
    padding: 20px 15px;
  }

  .nav-item {
    padding: 12px 15px;
  }
}

@media (max-width: 768px) {
  .admin-layout {
    flex-direction: column;
  }

  .admin-sidebar {
    width: 100%;
    order: 2;
  }

  .admin-main {
    order: 1;
  }

  .admin-header {
    padding: 15px 20px;
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }

  .header-right {
    width: 100%;
    justify-content: space-between;
  }

  .admin-nav {
    display: flex;
    overflow-x: auto;
    padding: 10px 0;
  }

  .nav-item {
    flex-shrink: 0;
    border-left: none;
    border-bottom: 3px solid transparent;
    min-width: 120px;
    flex-direction: column;
    text-align: center;
    padding: 10px 15px;
  }

  .nav-item.active {
    border-left: none;
    border-bottom-color: #48bb78;
  }

  .nav-icon {
    margin-right: 0;
    margin-bottom: 5px;
  }

  .nav-content {
    align-items: center;
  }

  .nav-description {
    display: none; /* 移动端隐藏描述 */
  }
}

@media (max-width: 480px) {
  .admin-content {
    padding: 15px;
  }

  .header-right {
    flex-direction: column;
    gap: 10px;
    align-items: flex-start;
  }

  .user-info {
    align-self: flex-end;
  }
}

/* 滚动条样式 */
.admin-content::-webkit-scrollbar {
  width: 6px;
}

.admin-content::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.admin-content::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.admin-content::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}
</style>